
package com.embedsky.administrator.mycardemulation;

import android.util.Log;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.embedsky.administrator.mycardemulation.message.SocketMessage;
import com.embedsky.administrator.mycardemulation.socket.IServerListener;
import com.embedsky.administrator.mycardemulation.socket.IServerManager;
import com.embedsky.administrator.mycardemulation.socket.ServerManager;

import java.util.ArrayList;
import java.util.List;

/**
 * @Des:
 * @Author: hang.zhao
 * @Date: 2023/2/28
 */
public class MainViewModel extends ViewModel {

    private String TAG = MainViewModel.class.getSimpleName();
    private IServerManager serverManager;
    private List<SocketMessage> socketMessageList = new ArrayList<>();
    private MutableLiveData<List<SocketMessage>> _messageList = new MutableLiveData<>();
    public LiveData<List<SocketMessage>> messageList = _messageList;
    private String clientIp;


    public void setMessageList(List<SocketMessage> messageList) {
        _messageList.postValue(messageList);
    }

    public void sendData(String data) {
        SocketMessage message = new SocketMessage();
        message.setData(data);
        message.setType(SocketMessage.MESSAGE_SEND);
        socketMessageList.add(message);
        serverManager.sendData(data, clientIp);
        _messageList.postValue(socketMessageList);
    }

    public List<SocketMessage> getSocketMessageList() {
        return _messageList.getValue();
    }

    public void stopClient() {
        if (serverManager != null) {
            serverManager.finish();
        }
    }


    public void initSocketServer() {
        serverManager = new ServerManager(8848, new IServerListener() {
            @Override
            public void onServerStart(int port) {
                Log.d(TAG, "onServerStart: ");

            }

            @Override
            public void onServerStartErr(int port) {

            }

            @Override
            public void onFinish() {

            }

            @Override
            public void parseData(byte[] bytes, String ip) {
                //收到客户端发来的消息，在这里解析响应
                //回复消息
                //serverManager.sendData();
                clientIp = ip;
                String message = new String(bytes);
                SocketMessage socketMessage = new SocketMessage();
                socketMessage.setData(message);
                socketMessage.setType(SocketMessage.MESSAGE_RECEIVE);
                socketMessageList.add(socketMessage);
                //runOnUiThread(() -> showToast("收到服务端响应消息:" + message));
                Log.d(TAG, "parseData: " + message);
                _messageList.postValue(socketMessageList);

            }

            @Override
            public void onClientDisconnect(String ip) {
                Log.d(TAG, "onClientDisconnect: ");
                //disConnect();
                stopClient();
            }
        });
    }
}

